package com.baoxin.chenxixc4coach.fragments.student;


import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.adapter.MyViewPagerAdapter;
import com.baoxin.chenxixc4coach.fragments.TitleFragment;
import com.baoxin.chenxixc4coach.fragments.student.subject.TestStutentFragment;
import com.baoxin.chenxixc4coach.fragments.student.subject.TrainStudentFragment;

import java.util.ArrayList;
import java.util.List;

public class SubjecFragment extends TitleFragment implements TabLayout.OnTabSelectedListener{
    private int subject_type;// 1 2 3 4对应科目1234
    private TrainStudentFragment trainStudentFragment;//在培学员
    private TestStutentFragment testStudentFragment;//考试学员
    private List<TitleFragment> fragments;

    private TabLayout tabLayout;
    private ViewPager viewPager;

    public void setSubject_type(int subject_type) {
        this.subject_type = subject_type;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_subject, container, false);

        tabLayout = view.findViewById(R.id.tab);
        viewPager = view.findViewById(R.id.view_pager);
        trainStudentFragment = new TrainStudentFragment();
        testStudentFragment = new TestStutentFragment();
        trainStudentFragment.setSubject_type(subject_type);
        testStudentFragment.setSubject_type(subject_type);
        fragments = new ArrayList<>();
        fragments.add(trainStudentFragment);
        fragments.add(testStudentFragment);

        viewPager.setAdapter(new MyViewPagerAdapter(getChildFragmentManager(), fragments));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);

        return view;
    }


    @Override
    public String getTabTitle() {
        if (subject_type == 1) {
            return "科一";
        } else if (subject_type == 2) {
            return "科二";
        } else if (subject_type == 3) {
            return "科三";
        } else {
            return "科四";
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
